<template>
  <div class="body-login">
    <el-form  :model="loginForm" class="login-container">
      <h1>欢迎登录</h1>
      <el-form-item>
        <el-input type="text" v-model="loginForm.username" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive } from "vue";
import { ElMessage } from "element-plus";
import { login } from "@/api/loginApi.js";
import { useRouter } from 'vue-router'

const loginForm = reactive({
  username: '',
  password: ''
})
const router = useRouter()

const handleLogin = async () => {
  if (!loginForm.username || !loginForm.password) {
    return ElMessage.error('请输入账号和密码')
  }

  let data = await login(loginForm)
  if (data.status && data.code === 200) {
    localStorage.setItem('token', data.data)
    ElMessage.success('登录成功')
    await router.push('/')
  }
}
</script>

<style scoped lang="less">
.body-login {
  width: 100%;
  height: 100vh; // 使用 vh 单位确保全屏高度
  background-image: url("../../assets/images/login_bg.png");
  background-size: cover; // 修改为 cover 确保图片覆盖整个屏幕
  background-position: center;
  background-repeat: no-repeat;
  display: flex; // 启用 Flexbox
  justify-content: center; // 水平居中
  align-items: center; // 垂直居中
  overflow: hidden;
}

.login-container {
  width: 300px;
  background-color: #fff;
  border: 1px solid #eaeaea;
  border-radius: 15px;
  padding: 35px 35px 15px 35px;
  box-shadow: 0 0 25px #cacaca;

  h1 {
    text-align: center;
    margin-bottom: 20px;
    color: #505450;
  }

  :deep(.el-form-item__content) {
    justify-content: center;
  }
}
</style>